#include "board.h"

struct CUSTOMVERTEX
{
	FLOAT x, y, z;  // The untransformed, 3D position for the vertex
	//DWORD color;

	float tu, tv;
};


#define MAX_SPRITES 1
#define SPRITE_WIDTH 45			// the width of all sprites
#define SPRITE_HEIGHT 45		// the height of all sprites

// this is the sprite structure
struct {
	RECT srcRect;				// holds the location of this sprite
	RECT srcRect2;				// in the source bitmap
	RECT srcRect3;

	int posX;					// the sprites X position
	int posY;					// the sprites Y position

	int posX2;
	int posY2;

	int posX3;
	int posY3;

} spriteStruct[MAX_SPRITES];

#define	D3DFVF_CUSTOMVERTEX	(D3DFVF_XYZ|D3DFVF_TEX1)	

long chaseX = 265, chaseY = 242, curXBlk, curYBlk;
long curXWht = 0, curYWht;

Board::Board()
{

}

Board::~Board()
{
}

void Board::initCube(void)
{
	D3DXCreateTextureFromFile( dxMgr.getDevice(), "test.bmp", &m_pTexture1);
	dxMgr.getDevice()->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
	dxMgr.getDevice()->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
}

bool Board::createCube(void)
{
	// Initialize three vertices for rendering a triangle	
	CUSTOMVERTEX g_Vertices[] =
	{
		// 1
		{ -32.0f,  32.0f, -64.0f, 0.0f, 0.0f},
		{  32.0f,  32.0f, -64.0f, 8.0f, 0.0f},
		{ -32.0f, -32.0f, -64.0f, 0.0f, 8.0f},
		{  32.0f, -32.0f, -64.0f, 8.0f, 8.0f},

	};

	// Create the vertex buffer.
    HRESULT hr;
	
	hr = dxMgr.getDevice()->CreateVertexBuffer(sizeof(g_Vertices) * sizeof(CUSTOMVERTEX),
		  							    0, 
										D3DFVF_CUSTOMVERTEX,
										D3DPOOL_DEFAULT, 
										&m_vertexBuffer, 
										NULL );
	if FAILED (hr)
		return false;

	// prepare to copy the vertices into the vertex buffer
	VOID* pVertices;
	// lock the vertex buffer
	hr = m_vertexBuffer->Lock(0, sizeof(g_Vertices), (void**)&pVertices, 0);

	// check to make sure the vertex buffer can be locked
	if FAILED (hr)
		return false;

	// copy the vertices into the buffer
    memcpy( pVertices, g_Vertices, sizeof(g_Vertices) );

	// unlock the vertex buffer
	m_vertexBuffer->Unlock();

	return true;
}

void Board::drawCube(void)
{
	D3DXMATRIX meshMat, meshScale, meshRotate, meshTranslate;

	dxMgr.createCamera(1.0f, 1500.0f);		// near clip plane, far clip plane
	dxMgr.moveCamera(D3DXVECTOR3(0.0f, 0.0f, -450.0f));
	dxMgr.pointCamera(D3DXVECTOR3(0.0f, 0.0f, 0.0f));

	dxMgr.getDevice()->SetStreamSource( 0, m_vertexBuffer, 0, sizeof(CUSTOMVERTEX) );
	dxMgr.getDevice()->SetFVF( D3DFVF_CUSTOMVERTEX );
	
	dxMgr.getDevice()->SetTexture(0, m_pTexture1);

	// set meshMat to identity
	D3DXMatrixIdentity(&meshMat);

	// set the rotation X
	D3DXMatrixRotationX(&meshRotate, timeGetTime()/500.0f);
			
	// set the scaling
	D3DXMatrixScaling(&meshScale, 2.8f, 2.8f, 2.8f);

	// set the Translation
	D3DXMatrixTranslation(&meshTranslate, 0.0, 0.0, 0.0);
			
	// multiple the scaling and translation matrices to create the meshMat matrix
	D3DXMatrixMultiply(&meshMat, &meshMat, &meshTranslate);
	D3DXMatrixMultiply(&meshMat, &meshMat, &meshScale);

	// transform the object in world space
	dxMgr.getDevice()->SetTransform(D3DTS_WORLD, &meshMat);

	dxMgr.getDevice()->DrawPrimitive( D3DPT_TRIANGLESTRIP,  0, 2 );
	dxMgr.getDevice()->DrawPrimitive( D3DPT_TRIANGLESTRIP,  4, 2 );
	dxMgr.getDevice()->DrawPrimitive( D3DPT_TRIANGLESTRIP,  8, 2 );
	dxMgr.getDevice()->DrawPrimitive( D3DPT_TRIANGLESTRIP, 12, 2 );
	dxMgr.getDevice()->DrawPrimitive( D3DPT_TRIANGLESTRIP, 16, 2 );
	dxMgr.getDevice()->DrawPrimitive( D3DPT_TRIANGLESTRIP, 20, 2 );

}

bool Board::initSprites(void)
{
	m_spriteSurface = dxMgr.getSurfaceFromBitmap("test2.bmp");
	if (m_spriteSurface == NULL)
		return false;

	for (int i=0; i < MAX_SPRITES; i++)
	{
		spriteStruct[i].srcRect.top = 0;
		spriteStruct[i].srcRect.left = i * SPRITE_WIDTH;
		spriteStruct[i].srcRect.right = spriteStruct[i].srcRect.left + 285;
		spriteStruct[i].srcRect.bottom = 220;
	}
	return true;

}

void Board::drawSprite(void)
{
	if(diMgr.isButtonDown(0))
	{
		chaseX += diMgr.getCurMouseX();
		chaseY += diMgr.getCurMouseY();
	}

	drawMoveBlk();
	drawMoveWht();

	for (int i=0; i < MAX_SPRITES; i++)
	{	
		spriteStruct[i].posX = chaseX;
		spriteStruct[i].posY = chaseY;

		RECT destRect;
		destRect.left = spriteStruct[i].posX;
		destRect.top = spriteStruct[i].posY;
		destRect.bottom = destRect.top + SPRITE_HEIGHT;
		destRect.right = destRect.left + SPRITE_WIDTH;
		dxMgr.blitToSurface(m_spriteSurface, &spriteStruct[i].srcRect, &destRect);
	}
}

void Board::loadPieceBlk(void)
{
	m_spriteSurface2 = dxMgr.getSurfaceFromBitmap("test2.bmp");

	for (int i=0; i < MAX_SPRITES; i++)
	{
		spriteStruct[i].srcRect2.top = 0;
		spriteStruct[i].srcRect2.left = 315;
		spriteStruct[i].srcRect2.right = spriteStruct[i].srcRect2.left + 230;
		spriteStruct[i].srcRect2.bottom = 220;
	}
}

void Board::drawMoveBlk(void)
{
	if(diMgr.isButtonDown(1))
	{
		curXBlk = chaseX;
		curYBlk = chaseY;
	}

	for (int i=0; i < MAX_SPRITES; i++)
	{	
		spriteStruct[i].posX2 = curXBlk;
		spriteStruct[i].posY2 = curYBlk;

		RECT destRect2;
		destRect2.left = spriteStruct[i].posX2;
		destRect2.top = spriteStruct[i].posY2;
		destRect2.bottom = destRect2.top + SPRITE_HEIGHT;
		destRect2.right = destRect2.left + SPRITE_WIDTH;
		dxMgr.blitToSurface(m_spriteSurface2, &spriteStruct[i].srcRect2, &destRect2);
	}
}

void Board::loadPieceWht(void)
{
	m_spriteSurface3 = dxMgr.getSurfaceFromBitmap("test2.bmp");

	for (int i=0; i < MAX_SPRITES; i++)
	{
		spriteStruct[i].srcRect3.top = 0;
		spriteStruct[i].srcRect3.left = 535;
		spriteStruct[i].srcRect3.right = spriteStruct[i].srcRect3.left + 230;
		spriteStruct[i].srcRect3.bottom = 220;
	}
}

void Board::drawMoveWht(void)
{
	if(diMgr.isButtonDown(2))
	{
		curXWht = chaseX;
		curYWht = chaseY;
	}

	for (int i=0; i < MAX_SPRITES; i++)
	{	
		spriteStruct[i].posX3 = curXWht;
		spriteStruct[i].posY3 = curYWht;

		RECT destRect3;
		destRect3.left = spriteStruct[i].posX3;
		destRect3.top = spriteStruct[i].posY3;
		destRect3.bottom = destRect3.top + SPRITE_HEIGHT;
		destRect3.right = destRect3.left + SPRITE_WIDTH;
		dxMgr.blitToSurface(m_spriteSurface3, &spriteStruct[i].srcRect3, &destRect3);
	}
}






